package controller.auth;

import com.jfinal.kit.StrKit;
import common.ErrorCode;
import common.MyBaseController;
import common.Util;
import model.User;

import java.util.List;

/**
 * Created by luob on 15/6/13.
 */
public class IndexController extends MyBaseController {
    public void index() {
        setAttr("current_user", User.me.findById(Util.getClientId(this), "uid, name"));
        renderJson();
    }

    public void login() {
        //valid param(uid), get user
        //check password
        List<User> u = User.me.getByParams(new String[]{"uid"}, new String[]{getPara()});
        if (u != null && u.size() > 0) {
            Util.login(this, u.get(0).getStr("id"));
            setAttr("error", ErrorCode.OK);
        } else {
            setAttr("error", ErrorCode.NOT_FOUND);
        }
        renderJson();
    }

    public void logout() {
        if (!StrKit.isBlank(Util.getClientId(this))) {
            Util.logout(this);
        }
        setAttr("error", ErrorCode.OK);
        renderJson();
    }
}
